package dji.ux.ext.setting.aircraft;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import dji.thirdparty.io.reactivex.android.schedulers.AndroidSchedulers;
import dji.ux.beta.R;
import dji.ux.beta.base.ConstraintLayoutWidget;
import dji.ux.beta.base.DJISDKModel;
import dji.ux.beta.base.SchedulerProvider;
import dji.ux.beta.base.uxsdkkeys.ObservableInMemoryKeyedStore;

public class ReturnAltitudePanel extends ConstraintLayoutWidget {
    private SchedulerProvider schedulerProvider;
    private EditText editText;
    private int mReturnHeight;
    private ReturnAltitudeModel returnAltitudeModel;

    public ReturnAltitudePanel(Context context) {
        super(context);
    }

    public ReturnAltitudePanel(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ReturnAltitudePanel(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void initView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        View view = inflate(context, R.layout.ext_panel_return_altitude, this);
        schedulerProvider = SchedulerProvider.getInstance();
        editText = view.findViewById(R.id.et_return_altitude);
        editText.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                getGOHomeHigh();
                setGOHomeHigh();
            }
            return true;
        });

        setClickable(true);
        setFocusable(true);

        if (!isInEditMode()) {
            returnAltitudeModel = new ReturnAltitudeModel(DJISDKModel.getInstance(),
                    ObservableInMemoryKeyedStore.getInstance(),
                    schedulerProvider);
        }

        setBackgroundColor(Color.BLACK);
        setPadding(25, 25, 25, 25);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (!isInEditMode()) {
            returnAltitudeModel.setup();
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        if (!isInEditMode()) {
            returnAltitudeModel.cleanup();
        }
        super.onDetachedFromWindow();
    }

    private void getGOHomeHigh() {
        final String s = editText.getText().toString().trim();
        if (!s.isEmpty()){
            mReturnHeight = Integer.valueOf(s);
            if (500 < mReturnHeight) {
                mReturnHeight = 500;
            } else if (mReturnHeight < 20) {
                mReturnHeight = 20;
            }
        }
    }

    private void setGOHomeHigh(){
        addDisposable(returnAltitudeModel.setReturnHeight(mReturnHeight).subscribe(() -> {
            // Do nothing
        }, logErrorConsumer(TAG, "setGOHomeHigh")));
    }

    @Override
    protected void reactToModelChanges() {
        addReaction(returnAltitudeModel.getReturnAltitude()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this::updateEditText));
    }

    private void updateEditText(float value) {
        editText.setText(String.valueOf(value));
    }

    @NonNull
    @Override
    public String getIdealDimensionRatioString() {
        return null;
    }
}
